﻿namespace Central.Linq {

	/// <summary>
	/// Класс настройки агрегирующей функции поля сущности
	/// </summary>
	public class AggregateFieldDefinition {
		#region конструктор

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFieldDefinition() {
			this.Function = AggregateFunction.Max;
		}

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFieldDefinition(string field)
			: this() {
			this.Field = field;
		}

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFieldDefinition(string field, AggregateFunction function)
			: this(field) {
			this.Function = function;
		}

		#endregion конструктор

		/// <summary>
		/// Наименование поля сущности
		/// </summary>
		public string Field { get; set; }

		/// <summary>
		/// Агрегирующая функция
		/// </summary>
		public AggregateFunction Function { get; set; }

		#region методы

		/// <summary>
		/// Сравнение объектов
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public override bool Equals(object obj) {
			return Equals(obj as AggregateFieldDefinition);
		}

		/// <summary>
		/// Сравнение объектов AggregateFunctionDefinition
		/// </summary>
		/// <param name="other"></param>
		/// <returns></returns>
		public bool Equals(AggregateFieldDefinition other) {
			if (other == null)
				return false;
			return ((this.Function == null && other.Function == null) || (this.Function != null && this.Function.Equals(other.Function))) && ((string.IsNullOrEmpty(this.Field) && string.IsNullOrEmpty(other.Field)) || (!string.IsNullOrEmpty(this.Field) && this.Field.Equals(other.Field)));
		}

		/// <summary>
		/// Получение Hash кода объекта
		/// </summary>
		/// <returns></returns>
		public override int GetHashCode() {
			int hash1 = this.Function == null ? 0 : this.Function.GetHashCode();
			int hash2 = this.Field == null ? 0 : this.Field.GetHashCode();
			return hash1 ^ hash2;
		}

		#endregion методы
	}
}